'use strict';

const Service = require('egg').Service;

    /*
    *   obj                 查询的具体数据
    *   pageObj             分页数据
    * pageObj.nowPage       从0开始
    */

class AddService extends Service {
    async getUser(obj = {}, pageObj = { nowPage: 0, pageSize: 8 }) {
        const { ctx } = this;
        return new Promise(async (resolve, reject) => {
            // 分页
            let { nowPage, pageSize } = pageObj;
            // 获取总长度 .count()
            let long = await ctx.model.User.find(obj).count();
            let FindArr = await ctx.model.User.find(obj).skip(nowPage * pageSize).limit(pageSize).sort({ role: -1 });
            if (FindArr.length <= 0) return resolve({ code: 201, msg: '用户数据较少', FindData: { FindArr, nowPage, pageSize, long } });
            return resolve({ code: 200, msg: '用户获取成功！', FindData: { FindArr, nowPage: nowPage || 0, pageSize, long } });
        })
    }

    async getVip(obj = {}, pageObj = { nowPage: 0, pageSize: 8 }) {
        const { ctx } = this;
        return new Promise(async (resolve, reject) => {
            // 分页
            let { nowPage, pageSize } = pageObj;
            // 获取总长度 .count()
            let long = await ctx.model.Vip.find(obj).count();
            let FindArr = await ctx.model.Vip.find(obj).skip(nowPage * pageSize).limit(pageSize).sort({ roleClass: -1 });
            if (FindArr.length <= 0) return resolve({ code: 201, msg: '用户数据较少', FindData: { FindArr, nowPage, pageSize, long } });
            return resolve({ code: 200, msg: '用户获取成功！', FindData: { FindArr, nowPage: nowPage || 0, pageSize, long } });
        })
    }
    /*
    *   obj         查询的具体数据
    *   pageObj     分页数据
    */
    async getOrder(obj = {}, pageObj = { nowPage: 0, pageSize: 8 }) {
        const { ctx } = this;
        return new Promise(async (resolve, reject) => {
            // 分页
            let { nowPage, pageSize } = pageObj;
            // 获取总长度 .count()
            let long = await ctx.model.Order.find(obj).count();
            let FindArr = await ctx.model.Order.find(obj).skip(nowPage * pageSize).limit(pageSize).sort({ orderName: -1 });
            if (FindArr.length <= 0) return resolve({ code: 201, msg: '数据较少请创建项目', FindData: {FindArr, nowPage, pageSize, long} });
            return resolve({ code: 200, msg: '消费项目获取成功！', FindData: {FindArr, nowPage: nowPage || 0, pageSize, long} });
        })
    }
}

module.exports = AddService;
